/*
 * Air Window
 * @author helio
 */

/**
 * @class Ext.ux.AirWindow
 * @extends Ext.Viewport
 * Custerm Window of the System.
 */
Ext.ux.AirWindow = Ext.extend(Ext.Viewport, {
	layout: 'fit',
	draggable: true,
	initComponent: function(){
		this.panel = new Ext.ux.AirWindow.CoreWindow(this);
		Ext.apply(this.panel, {
			itemId: 'panel'
		});
		
		this.items = [this.panel];
		Ext.ux.AirWindow.superclass.initComponent.call(this);
		this.panel = this.getComponent('panel');
	},
	
	afterRender: function(){
		Ext.ux.AirWindow.superclass.afterRender.call(this);
		// error that element does not exist unless deferred 1ms.
		(function(){
			this.panel.dd = new Ext.ux.AirWindow.DD(this.panel);
		}).defer(1, this);
		
		if (this.initialise) {
			this.moveToCenter();
		}
	},
	
	moveToCenter : function() {
		window.nativeWindow.x = (air.Capabilities.screenResolutionX - window.nativeWindow.width)/2;
		window.nativeWindow.y = (air.Capabilities.screenResolutionY - window.nativeWindow.height)/2;
	}
});

/**
 * @class Ext.ux.AirWindow.DD
 * @extends Ext.dd.DD
 * The Drag and DoubleClick event of the Window.
 */
Ext.ux.AirWindow.DD = Ext.extend(Ext.dd.DD, {
	constructor: function(vp){
		Ext.ux.AirWindow.DD.superclass.constructor.call(this, vp.el.id, 'ChromeVp-' + vp.id);
		this.setHandleElId(vp.header.id);
		Ext.get(vp.header.id).dom.ondblclick = this.doubleClick.createDelegate(vp);
	},
	moveOnly: true,
	headerOffsets: [100, 25],
	startDrag: function(){
		window.nativeWindow.startMove();
	},
	doubleClick: function() {
		if(this.isMax && this.maximizable) {
			this.restore();
		} else if(this.maximizable) {
			this.maximize();
		}
	},
	onDrag: Ext.emptyFn,
	b4Drag: Ext.emptyFn,
	endDrag: Ext.emptyFn
});

/**
 * @class Ext.ux.AirWindow.CoreWindow
 * @extends Ext.Panel
 * The content of the window.
 * icon,title,tools.
 */
Ext.ux.AirWindow.CoreWindow = Ext.extend(Ext.Panel, {
    layout: 'fit',
	border: true,
	//frame : true,
	plain : true,
    initComponent: function(){
   
        this.tools = [{
            id: 'gear',
            qtip: 'URL设置',
            hidden: !this.gear,
            handler: this.showUrlSettingWindow,
            scope: this
        }, {
            id: 'minimize',
            qtip: '窗口最小化',
            handler: this.minimize,
            scope: this
        }, {
            id: 'restore',
            qtip: '还原窗口',
            hidden: true,
            handler: this.restore,
            scope: this
        }, {
            id: 'maximize',
            qtip: '窗口最大化',
            hidden: !this.maximizable,
            handler: this.maximize,
            scope: this
        }, {
            id: 'close',
            qtip: '关闭/退出',
            handler: this.exit,
            scope: this
        }];
		
        this.items = [this.panel];
        
        Ext.ux.AirWindow.CoreWindow.superclass.initComponent.call(this);
        
        var nativeApp = air.NativeApplication.nativeApplication;
        nativeApp.addEventListener(air.Event.EXITING, this.onExiting.createDelegate(this));
        //nativeApp.addEventListener(air.Event.NETWORK_CHANGE, this.onClosing.createDelegate(this)); 
        
        var root = Ext.air.NativeWindow.getRootWindow();
        root.addEventListener(air.Event.CLOSING, this.onClosing.createDelegate(this));
        root.addEventListener(air.Event.ACTIVATE, function(ev){
            Ext.air.NativeWindowManager.each(function(win){
                win.instance.orderToFront();
            });
        });
    },
    
    exit: function(){
        var exitingEvent = new air.Event(air.Event.EXITING, false, true);
        air.NativeApplication.nativeApplication.dispatchEvent(exitingEvent);
        if (!exitingEvent.isDefaultPrevented()) {
            air.NativeApplication.nativeApplication.exit();
        }
    },
    
    minimize: function(){
        nativeWindow.minimize();
    },
    
    maximize: function(event){
        nativeWindow.maximize();
        this.getTool('maximize').hide();
        this.getTool('restore').show();
        this.isMax = true;
    },
    
    restore: function(){
        nativeWindow.restore();
        this.getTool('restore').hide();
        this.getTool('maximize').show();
        this.isMax = false;
    },
    
    onExiting: function(exitEvent){
        var winClosingEvent;
        Ext.air.NativeWindowManager.each(function(win){
            winClosingEvent = new air.Event(air.Event.CLOSING, false, true);
            win.instance.dispatchEvent(winClosingEvent);
            if (!winClosingEvent.isDefaultPrevented()) {
                win.instance.close();
            }
            else {
                exitEvent.preventDefault();
            }
        });
    },
    
    // when user closes main window exit app
    onClosing: function(closeEvent){
        this.exit();
    },
    
    showUrlSettingWindow: function() {
    	this.inputWin = new Ext.Window({
	 		
		 	title: '服务URL设置',
			closable: true,
			frame: true,
			//plain:true,
			width: 400,
			height: 200,
			layout: 'border',
			closeAction :'close',
			items: [{
    			region: 'center',
    			margins: '50 10 0 20',
				xtype : "urlsetting"
			}]
		});
		
		this.inputWin.show();
	}
});